package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.BusinessException;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.BaiduAuditUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.dto.ShopDto;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;


    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;


    @Override
    public void settlement(Shop shop) {
        //一：检验 - 空值校验：前端的js校验容易被禁用，不安全，后端也要做校验
        if(StringUtils.isEmpty(shop.getName())){ //空和空字符串返回true
            throw new BusinessException("提交资料不能为空!!!");
        }

        //加入店铺名称的校验
        if(!BaiduAuditUtils.TextCensor((shop.getName()))){
            throw  new BusinessException("店铺名称不合法，请重新输入");
        }
        //加入图片(店铺Logo)的校验
        if(!StringUtils.isEmpty(shop.getLogo())){
            if(!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
                throw  new BusinessException("Logo违规！");
            }
        }


        //二：校验 - 当前店铺是否入驻过
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if(dbShop != null){//查到了 = 入驻过
            throw new BusinessException("请勿重复入驻，忘记密码请联系管理员!!!");
        }

        //三：数据库操作
        //1.保存t_employee员工信息 - 生成自增id
        Employee admin = shop.getAdmin();
        employeeMapper.add(admin);//只要添加之后，就生成了自增id

        //2.生成店铺的自增id
        shop.setAdminId(admin.getId());
        shopMapper.add(shop);

        //3.将店铺的shopid设置到employee对象，但是因为上面已经添加过id了，所以这里只能修改employee信息
        admin.setShopId(shop.getId()); //admin对象：id，shopId
        employeeMapper.update(admin);


    }

    //审核通过修改状态
    @Override
    public void pass(ShopAuditLog log) throws MessagingException {

        //利用log找到log的id 然后根据这个id找到店铺
        Shop shop = shopMapper.findById(log.getShopId());

        //设置店铺审核状态为通过
        shop.setState(2);
        //更新店铺信息
        shopMapper.update(shop);
        //log.setAuditId("审核人是谁=登陆的员工");
        //保存审核日志
        log.setState(2);

        shopAuditLogMapper.add(log);

        //创建复杂邮件对象
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //发送复杂邮件的工具类                                           能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1069494754@qq.com");//发送方
        helper.setSubject("店铺激活邮件");//                                                                                                                                                                               能够在内容中编写html
        helper.setText("<h1>你的店铺已经注册!!!</h1><a href='http://localhost:8080/shop/active/"+log.getShopId()+"'>点击该链接激活</a>",true);

        //发给谁 发给店铺管理员的邮箱
        // 通过店铺根据管理员id找到管理员  再在employee里找到这个店铺管理员
        Employee admin = employeeMapper.findById(shop.getAdminId());
        //然后发到管理员的邮箱里
        helper.setTo(admin.getEmail());

        //添加附件
        helper.addAttachment("aa.bmp",new File("C:\\Users\\Administrator\\Desktop\\aa.bmp"));
        //收件人
        helper.setTo("1069494754@qq.com");
        mailSender.send(mimeMessage);

    }
    //审核未通过驳回
    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        //改状态 保存审核日志 发邮件
        Shop shop = shopMapper.findById(log.getShopId());
        shop.setState(4);
        shopMapper.update(shop);

        //保存审核日志
        log.setState(4);
        //log.setAuditId("审核人=登陆人");
        shopAuditLogMapper.add(log);

        //发邮件
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8");
        helper.setFrom("659769497@qq.com");
        helper.setSubject("店铺激活失败通知");
        //true - 能够再内容中编写html标签 - 会解析               跳转到前端页面重新修改信息
        helper.setText("<h3>你的店铺审核未通过，请<a href='http://localhost:8081/#/regiseterEdit/"+log.getShopId()
                +"'>点击这里</a>修改入驻信息重新提交</h3>", true);
        //店铺管理员
        Employee admin = employeeMapper.findById(shop.getAdminId());
        helper.setTo(admin.getEmail());
        //发送
        mailSender.send(mimeMessage);

    }
    //首页柱状图
    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }

    @Override
    public void active(Long shopId) {
        Shop shop = shopMapper.findById(shopId);
        shop.setState(3);
        shopMapper.update(shop);
    }
}
